package com.ctgu.tengyun.service.customer.impl;

import com.ctgu.tengyun.dao.mybatis.CustomerInfoMapper;
import com.ctgu.tengyun.domain.CustomerInfo;
import com.ctgu.tengyun.domain.CustomerInfoExample;
import com.ctgu.tengyun.service.customer.CustomerInfoService;
import com.ctgu.tengyun.util.CollectionsUtil;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/7.
 */
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

    @Resource
    private CustomerInfoMapper mapper;

    @Override
    public int insertSelective(CustomerInfo record) {
        return mapper.insertSelective(record);
    }

    @Override
    public CustomerInfo selectByContactNo(String contactNo) {
        CustomerInfoExample example = new CustomerInfoExample();
        CustomerInfoExample.Criteria criteria = example.createCriteria();
        criteria.andContactNoEqualTo(contactNo);

        List<CustomerInfo> customerInfoList =  mapper.selectByExample(example);
        if(CollectionsUtil.isNotEmpty(customerInfoList)){
            return customerInfoList.get(0);
        }

        return null;
    }

    @Override
    public Integer countCustomer(CustomerInfo customerInfo) {
        CustomerInfoExample example = new CustomerInfoExample();
        CustomerInfoExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotEmpty(customerInfo.getName())){
            criteria.andNameLike("%" + customerInfo.getName() +"%");
        }
        if(StringUtils.isNotEmpty(customerInfo.getContactNo())){
            criteria.andContactNoLike("%" + customerInfo.getContactNo() +"%");
        }

        return mapper.countByExample(example);
    }

    @Override
    public List<CustomerInfo> selectCustomerList(CustomerInfo customerInfo, Integer pageNo, Integer pageSize) {
        CustomerInfoExample example = new CustomerInfoExample();
        CustomerInfoExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotEmpty(customerInfo.getName())){
            criteria.andNameLike("%" + customerInfo.getName() +"%");
        }
        if(StringUtils.isNotEmpty(customerInfo.getContactNo())){
            criteria.andContactNoLike("%" + customerInfo.getContactNo() +"%");
        }
        
        example.setStartIndex((pageNo - 1) * pageSize);
        example.setPageSize(pageSize);

        return mapper.selectByExample(example);
    }

    @Override
    public CustomerInfo selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerInfo> selectByIds(Collection<Long> ids) {
        if(!CollectionsUtil.isNotEmpty(ids)){
            return Collections.emptyList();
        }

        CustomerInfoExample example = new CustomerInfoExample();
        CustomerInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIdIn(new ArrayList<>(ids));
        return mapper.selectByExample(example);
    }

    @Override
    public int updateByPrimaryKeySelective(CustomerInfo record) {
        return mapper.updateByPrimaryKeySelective(record);
    }
}
